﻿using ICSharpCode.AvalonEdit.Document;
using System.Windows.Input;

namespace JsonCodeGenerator
{
    internal class MainViewModel : NotifyPropertyChanged
    {
        private bool _isJsonPropertyAttribute;
        private bool _isJsonPropertyNameAttribute;
        private bool _isXmlComment;
        private bool _isTitleCase;
        private bool _isToCamelCase;
        private string? _commentType;
        private TextDocument _jsonText = new();
        private TextDocument _sharpText = new();

        public MainViewModel()
        {
            GeneratorCommand = new ActionCommand(Generator);
            JsonFormateCommand = new ActionCommand(JsonFormate);
        }

        /// <summary>
        /// 是否添加 JsonProperty 特性
        /// </summary>
        public bool IsJsonPropertyAttribute { get => _isJsonPropertyAttribute; set => SetProperty(ref _isJsonPropertyAttribute, value); }

        /// <summary>
        /// 是否添加 JsonPropertyName 特性
        /// </summary>
        public bool IsJsonPropertyNameAttribute { get => _isJsonPropertyNameAttribute; set => SetProperty(ref _isJsonPropertyNameAttribute, value); }

        /// <summary>
        /// 是否添加xml注释
        /// </summary>
        public bool IsXmlComment { get => _isXmlComment; set => SetProperty(ref _isXmlComment, value); }

        /// <summary>
        /// 小写转大写
        /// </summary>
        public bool IsTitleCase { get => _isTitleCase; set => SetProperty(ref _isTitleCase, value); }

        /// <summary>
        /// xml注释类型
        /// </summary>
        public string? CommentType { get => _commentType; set => SetProperty(ref _commentType, value); }

        /// <summary>
        /// Json文本
        /// </summary>
        public TextDocument JsonText { get => _jsonText; set => SetProperty(ref _jsonText, value); }

        /// <summary>
        /// 转换驼峰命名
        /// </summary>
        public bool IsToCamelCase { get => _isToCamelCase; set => SetProperty(ref _isToCamelCase, value); }

        /// <summary>
        /// C#文本
        /// </summary>
        public TextDocument SharpText { get => _sharpText; set => SetProperty(ref _sharpText, value); }

        public ICommand GeneratorCommand { get; set; }

        public ICommand JsonFormateCommand { get; set; }

        private void Generator(object? parameter)
        {
            if (parameter is MainViewModel vm && vm.SharpText != null)
            {
                var config = new GeneratorConfig()
                {
                    IsJsonPropertyAttribute = vm.IsJsonPropertyAttribute,
                    IsJsonPropertyNameAttribute = vm.IsJsonPropertyNameAttribute,
                    IsTitleCase = vm.IsTitleCase,
                    IsXmlComment = vm.IsXmlComment,
                    IsToCamelCase = vm.IsToCamelCase,
                    CommentType = JsonCodeGenerator.CommentType.Name,
                };

                vm.SharpText.Text = GeneratorService.CodeGenerator(vm.JsonText?.Text ?? "", config);
            }
        }

        private void JsonFormate(object? parameter)
        {
            if (parameter is MainViewModel vm)
            {
                vm.JsonText.Text = GeneratorService.JsonFormate(vm.JsonText?.Text ?? "");
            }
        }
    }
}
